<?php 
	namespace app\common\model;
	use think\Db;
	use pay\payInterface_native\Request;
	require('D:\wnmp\nginx\www\PAY\extend\phpqrcode.php');
	/**
	* 订单验证
	*/
	class Orderconvert
	{
		public function get_list($data)
		{
			$result = array('state'=>'success', 'msg'=>'成功');
			//参数处理
			$validate = $this->data_validate($data);
			if($validate['result'] == 'success')
			{//参数处理完毕
				$apitype = $data['type']; //中转类型
				$apiorderid = $data['orderid']; //订单号
				$srcurl = $_SERVER['HTTP_REFERER']; //中转前原始url
				$logseq = $data['logseq']; //日志序号
				//$sql = "exec proc_orderbank_checkconvert @apiorderid='$apiorderid',@apitype='$apitype',@srcurl='$srcurl',@logseq='$logseq'";
				echo "success";
				$sql = "exec proc_orderbank_checkconvert @apiorderid='$apiorderid',@apitype='$apitype',@srcurl='$srcurl',@logseq='$logseq'";
				$res = Db::query($sql);
				// $res = Db::query();
				$m = json_encode($res);
				file_put_contents('/data/wwwroot/PAY/runtime/log/db.log', $m);
				if(empty($res))
				{//重复提交
					$result = array('state'=>'fail', 'msg'=>'error:2001 跳转重复');
					return $result;
				}
				else
				{//未提交,向上游发起请求
					$data_order = $data;
					$push = $this->push_to_pay($data_order);
				}
			}
			else
			{//返回错误
				return $validate;
			}
		}	

		//参数处理
		public function data_validate($data)
		{
			$result = array('result'=> 'success', 'msg'=> '成功');
			$file = array('rbType', 'orderid', 'logseq');
			//参数过滤
			$param = $this->arr_filter($data, $file);
			//排序
			ksort($param);
			//获取秘钥
			$key = '!@#qweASDzxc';
			$param_to_url = http_build_query($param);
			$param_to_url = urldecode($param_to_url);
			$param_to_url = $param_to_url . $key;
			$param_to_md5 = md5($param_to_url);
			$param_to_md5 == $data['sign'] ? : $result = array( 'result' => 'success', 'msg' => '签名错误');
			return $result;
		}

		/**
		 * 数据过滤
		 */
		public function arr_filter($arr, $file)
		{

			foreach ($arr as $key=>$value) 
			{
				if( !in_array($key, $file) )
				{
					unset($arr[$key]);
				}
			}
			return $arr;
		}

		/**
		 * 发起支付请求
		 */
		public function push_to_pay($data)
		{
			//参数重新包装
			$request = new Request();
			$result = $request->index($data);
			dump($result);
			die();
			//定义纠错级别
			$errorLevel = "L";
			//定义生成图片宽度和高度;默认为3
			$size = "4";
			//定义生成内容
			$content="微信公众平台：思维与逻辑;公众号:siweiyuluoji";
			// $filename = 'D:/wnmp/nginx/www/PAY/application/common/model/'.microtime().'.png';  
			//调用QRcode类的静态方法png生成二维码图片//
			// \QRcode::png($content, $filename, $errorLevel, $size);
			//生成网址类型
			$url="http://jingyan.baidu.com/article/48a42057bff0d2a925250464.html";
			$url.="rn";
			$url.="http://jingyan.baidu.com/article/acf728fd22fae8f8e510a3d6.html";
			$url.="rn";
			$url.="http://jingyan.baidu.com/article/92255446953d53851648f412.html";
			$dd = \QRcode::png($url, false, $errorLevel, $size);
			echo $dd;
			die();
		}
	}
?>
